/*
 * @(#)UserCookie.java   1.0  2019年1月29日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.dts.service.core;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swift.core.model.HttpServiceRequest;
import com.swift.core.model.ServiceRequest;
import com.swift.core.session.AbstractSession;
import com.swift.core.session.SessionCrypt;

/**
 * 添加说明 
 * @author zhengjiajin
 * @version 1.0 2019年1月29日
 */
@Service
public class UserCookie {

 // cookie的生存期
    static final int MAX_AGE = 60 * 60 * 24;

    @Autowired
    private SessionCrypt sessionCrypt;
    /**
     * 添加cookie
     */
    public void addSSoCookie(ServiceRequest request, AbstractSession sessionUser) {
        HttpServletRequest servletRequest = ((HttpServiceRequest) request).getRequest();
        HttpServletResponse servletResponse = ((HttpServiceRequest) request).getResponse();
        Cookie sessionCookie = createCookie(AbstractSession.SESSION_NAME,sessionCrypt.encrypt(sessionUser), servletRequest);
        servletResponse.addCookie(sessionCookie);
        for(String key:sessionUser.keySet()) {
            Cookie userIdCookie = createCookie(key,sessionUser.getString(key), servletRequest);
            if(!AbstractSession.SESSION_NAME.equalsIgnoreCase(userIdCookie.getName())) {
                servletResponse.addCookie(userIdCookie);
            }
        }
    }

    private Cookie createCookie(String cookieKey, String cookieValue, HttpServletRequest servletRequest) {
        Cookie cookie = getCookie(cookieKey, servletRequest);
        return setCookie(cookie, cookieKey, cookieValue, servletRequest);
    }
    
    
    private Cookie getCookie(String cookieKey, HttpServletRequest servletRequest) {
        Cookie[] cookies = servletRequest.getCookies();  
        if (null==cookies) {  
            return null;
        } else {  
            for(Cookie cookie : cookies){  
                if(cookie.getName().equals(cookieKey)){  
                    return cookie;
                }  
            }  
        }
        return null;
    }

    private Cookie setCookie(Cookie sessionCookie,String cookieKey, String cookieValue, HttpServletRequest servletRequest) {
        if(sessionCookie==null) {
            sessionCookie = new Cookie(AbstractSession.SESSION_NAME, cookieValue);
        } else {
            sessionCookie.setValue(cookieValue);
        }
        sessionCookie.setPath("/");
        if ("https".equals(servletRequest.getScheme())) {
            sessionCookie.setSecure(true);
        }
        sessionCookie.setHttpOnly(false);
        sessionCookie.setMaxAge(MAX_AGE);
        return sessionCookie;
    }


    /**
     * 清除cookie
     */
    public void removeCookie(ServiceRequest req) {
        HttpServiceRequest request = (HttpServiceRequest) req;
        Cookie[] cookies = request.getRequest().getCookies();
        if(cookies==null) return;
        for (int i = 0; i < cookies.length; i++) {
            cookies[i].setValue(null);
            cookies[i].setPath("/");
            cookies[i].setMaxAge(0);
            HttpServletResponse servletResponse = ((HttpServiceRequest) request).getResponse();
            servletResponse.addCookie(cookies[i]);
        }
    }
}
